import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const api = {
	/* 状态属性值 */
	state:()=>({
		address:{},//选择的地址
		accessToken:'',	//验证Token
		refreshToken:'',	//刷新用Token
		gps:null,	//定位信息
		type_tree:null,
		type_list:[],
		scenery:{name:"景点"},
		goods:{name:"美食"},
		changeUserInfo:false
	}),
	/* 计算属性 */
    getters: {
		hasGps:(state)=>{
			if(state.gps){
				return state.gps
			}else{
				return false
			}
		},
		loadGps:(state)=>{
			return state.gps
		},
		loadStoreGps:()=>{
			let gps=uni.getStorageSync('gps')
			if(typeof(gps)=='object'){
				return gps
			}else{
				return {}
			}
		},
		uid:(state)=>{
			return uni.getStorageSync('Uid')
		},
		user_detail:(state)=>{
			if(state.user_detail!=null){
				return state.user_detail
			}else{
				let user_detail=uni.getStorageSync('user_detail')
				if(typeof(user_detail)!='string'){
					state.user_detail=user_detail		
					return state.user_detail
				}else{
					return {
						school:'本校',
						schoolId:0,
						academy:'本院',
						academyId:0
					}
				}
			}			
		},
		type_tree:function(state){
			if(state.type_tree!=null){
				return state.type_tree
			}else{
				let type_tree=uni.getStorageSync('type_tree')
				let now=new Date().getTime()
				if(type_tree.expire&&type_tree.expire>now){
					state.type_tree=type_tree.tree
					return state.type_tree
				}else{
					return false				
				}
			}
		},
		type_list:function(state){
			return state.type_list
		},
		scenery:function(state){
			return state.scenery
		},
		goods:function(state){
			return state.goods
		},
		isUserInfoChnage(state){
			return state.changeUserInfo
		}
	},
    /* 计算方法 */
	mutations:{
		//设置地址信息
		SET_ADDRESS(state,address){
			state.address=address
		},
		SET_GPS(state,gps){
			state.gps=gps
		},
		SET_TYPE_LIST(state,typeList){
			state.type_list=typeList
		},
		SET_SCENERY(state,scenery){
			state.scenery=scenery
		},
		SET_GOODS(state,goods){
			state.goods=goods
		},
		CHANGE_USER_INFO(state,change){
			state.changeUserInfo=change
		}
	},
	/*
		导步计算方法
		定义 incrementAsync  :function ({ commit, state,dispatch,rootState  },data){commit('increment',data)}
		使用 store.dispatch('incrementAsync', data) or  store.commit({type:'incrementAsync',...data})
	 */
	actions: {
	}
}

export default api